/**
 * 
 */
package org.vsg.security;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.jsecurity.authc.Account;
import org.jsecurity.authz.Permission;
import org.jsecurity.subject.PrincipalCollection;
import org.jsecurity.subject.SimplePrincipalCollection;
import org.vsg.security.authz.permission.AbstractResourcePermission;
import org.vsg.security.model.domain.ResourceAssociation;
import org.vsg.security.model.domain.SysOperation;
import org.vsg.security.model.domain.SysUser;
import org.vsg.security.vo.SecRole;

/**
 * @author Bill Vison
 *
 */
public class AccountUser implements Account {
	
	private SysUser user;
	
	private Set<SecRole> roles;
	
	private Set<ResourceAssociation> resAsso;

	private Set<Permission> grantPermission = new HashSet<Permission>();
	
	
	public AccountUser(SysUser user) {
		this(user , new HashSet<SecRole>() , new HashSet<ResourceAssociation>() );
	}
	
	public AccountUser(SysUser user , Set<SecRole> grantRoles , Set<ResourceAssociation> grantAsso) {
		this.user = user;
		this.roles = grantRoles;
		this.resAsso = grantAsso;
		
		initialize();

	}
	
	public void initialize() {
		
		ResourceAssociation reso = null;
		AbstractResourcePermission permission = null;
		for (Iterator<ResourceAssociation> assoIter = resAsso.iterator() ; assoIter.hasNext() ; ) {
			reso = assoIter.next();
			SysOperation opera = reso.getOperation();
			
			String className = opera.getOperation();

			try {
				Class cls = Class.forName(className);
				permission = (AbstractResourcePermission)cls.newInstance();
				permission.setResource( reso.getResource() );
				
				// --- init permssion ---
				grantPermission.add( permission );
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		
	}

	/**
	 * validate password
	 * @see org.jsecurity.authc.AuthenticationInfo#getCredentials()
	 */
	@Override
	public Object getCredentials() {
		// TODO Auto-generated method stub
		return user.getPassword();
	}

	/* (non-Javadoc)
	 * @see org.jsecurity.authc.AuthenticationInfo#getPrincipals()
	 */
	@Override
	public PrincipalCollection getPrincipals() {
		// TODO Auto-generated method stub
        return new SimplePrincipalCollection(new Integer(user.getId()), AccountAuthorizingRealm.class.getName());
	}

	/* (non-Javadoc)
	 * @see org.jsecurity.authz.AuthorizationInfo#getObjectPermissions()
	 */
	@Override
	public Collection<Permission> getObjectPermissions() {
		// TODO Auto-generated method stub
		return grantPermission;
	}

	/* (non-Javadoc)
	 * @see org.jsecurity.authz.AuthorizationInfo#getRoles()
	 */
	@Override
	public Collection<String> getRoles() {
		// TODO Auto-generated method stub
		// --- show role code ---
		Set<String> roleCodes = new HashSet<String>();
		for (Iterator<SecRole> iterRoles = roles.iterator() ; iterRoles.hasNext();) {
			roleCodes.add( iterRoles.next().getRole().getCode() );
		}
		return roleCodes;
	}

	/* (non-Javadoc)
	 * @see org.jsecurity.authz.AuthorizationInfo#getStringPermissions()
	 */
	@Override
	public Collection<String> getStringPermissions() {
		// TODO Auto-generated method stub
		Set<String> permStrins = new HashSet<String>();
		return permStrins;
	}

}
